package com.sevattal.service.leaves.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sevattal.common.base.result.ResultCodeEnum;
import com.sevattal.service.base.exception.SevattalException;
import com.sevattal.service.leaves.entity.Dept;
import com.sevattal.service.leaves.entity.vo.DeptVo;
import com.sevattal.service.leaves.mapper.DeptMapper;
import com.sevattal.service.leaves.service.DeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author Sevattal
 * @since 2021-08-28
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {

    @Override
    public boolean deleteDept(String id) {
        //查找数据，若存在则删除
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("f_dId", id);
        int result = baseMapper.delete(queryWrapper);
        if (result != 0) {
            return true;
        } else {
            return false;

        }
    }

    @Override
    public boolean createDept(Dept dept) {
        // 判断DID是否存在，判断名称是否存在
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("f_dId",dept.getFDid()).or().eq("f_dName",dept.getFDname());
        int result = baseMapper.selectCount(queryWrapper);
        if (result > 0) {
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        baseMapper.insert(dept);
        return true;
    }

    @Override
    public boolean updateDept(Dept dept) {
        // 判断数据库中是否存在，存在则更新
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("f_dId",dept.getFDid());
        int count = baseMapper.selectCount(queryWrapper);
        if (count == 0) {
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        baseMapper.update(dept,queryWrapper);
        return true;
    }

    @Override
    public DeptVo getByDeptId(String id) {
        DeptVo deptVo = baseMapper.getByDeptId(id);
        return deptVo;
    }

    @Override
    public List<DeptVo> getDeptList() {
        List<DeptVo> deptVoList = baseMapper.getDeptList();
        return deptVoList;
    }
}
